import UIKit

class SettingsView: UIView {

  let stepOneLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "1.系统设置-打开键盘"
    label.font = .systemFont(ofSize: 18, weight: .bold)
    label.textAlignment = .center
    return label
  }()

  let stepOneDescLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "为保障正常使用，请按下图说明完成设置"
    label.font = .systemFont(ofSize: 14)
    label.textColor = .gray
    label.textAlignment = .center
    return label
  }()

  let settingsScrollView: UIScrollView = {
    let scrollView = UIScrollView()
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.isPagingEnabled = true
    scrollView.showsHorizontalScrollIndicator = false
    return scrollView
  }()

  let pageControl: UIPageControl = {
    let pageControl = UIPageControl()
    pageControl.translatesAutoresizingMaskIntoConstraints = false
    pageControl.numberOfPages = 2
    pageControl.currentPage = 0
    pageControl.pageIndicatorTintColor = .gray
    pageControl.currentPageIndicatorTintColor = .orange
    return pageControl
  }()

  lazy var settingsButton: UIButton = {
    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("去设置中开启", for: .normal)
    button.setTitleColor(.white, for: .normal)
    button.backgroundColor = .systemBlue
    button.layer.cornerRadius = 8
    return button
  }()

  override init(frame: CGRect) {
    super.init(frame: frame)
    setupUI()
    setupSettingsImages()
  }

  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  private func setupUI() {
    settingsScrollView.delegate = self
    pageControl.addTarget(self, action: #selector(pageControlDidChange(_:)), for: .valueChanged)

    addSubview(stepOneLabel)
    addSubview(stepOneDescLabel)
    addSubview(settingsScrollView)
    addSubview(pageControl)
    addSubview(settingsButton)

    NSLayoutConstraint.activate([
      stepOneLabel.topAnchor.constraint(equalTo: topAnchor, constant: 16),
      stepOneLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
      stepOneLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20),

      stepOneDescLabel.topAnchor.constraint(equalTo: stepOneLabel.bottomAnchor, constant: 8),
      stepOneDescLabel.leadingAnchor.constraint(equalTo: stepOneLabel.leadingAnchor),
      stepOneDescLabel.trailingAnchor.constraint(equalTo: stepOneLabel.trailingAnchor),

      settingsScrollView.topAnchor.constraint(equalTo: stepOneDescLabel.bottomAnchor, constant: 16),
      settingsScrollView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 50),
      settingsScrollView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -50),
      settingsScrollView.heightAnchor.constraint(
        equalTo: settingsScrollView.widthAnchor, multiplier: 1.1),

      pageControl.topAnchor.constraint(equalTo: settingsScrollView.bottomAnchor, constant: 8),
      pageControl.centerXAnchor.constraint(equalTo: centerXAnchor),

      settingsButton.topAnchor.constraint(equalTo: pageControl.bottomAnchor, constant: 12),
      settingsButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0),
      settingsButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0),
      settingsButton.heightAnchor.constraint(equalToConstant: 44),
      settingsButton.bottomAnchor.constraint(equalTo: bottomAnchor),  // 最后一个
    ])
  }

  private func setupSettingsImages() {
    let images = ["setting_1", "setting_2"]
    let imageWidth = UIScreen.main.bounds.width - 140
    for (index, imageName) in images.enumerated() {
      let imageView = UIImageView(image: UIImage(named: imageName))
      imageView.contentMode = .scaleAspectFill
      imageView.clipsToBounds = true
      imageView.frame = CGRect(
        x: CGFloat(index) * imageWidth, y: 0, width: imageWidth, height: imageWidth * 1.1)
      settingsScrollView.addSubview(imageView)
    }
    settingsScrollView.contentSize = CGSize(width: CGFloat(images.count) * imageWidth, height: 0)
  }

  @objc private func pageControlDidChange(_ sender: UIPageControl) {
    let x = CGFloat(sender.currentPage) * settingsScrollView.frame.width
    settingsScrollView.setContentOffset(CGPoint(x: x, y: 0), animated: true)
  }
}

extension SettingsView: UIScrollViewDelegate {
  func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let pageWidth = scrollView.frame.width
    let currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
    pageControl.currentPage = currentPage
  }
}
